import QtQuick 2.0
import QtQuick.Window 2.1

Window {
    width: 200; height: element_height * context_count + (view.spacing * (context_count - 1))
    flags: Qt.FramelessWindowHint | Qt.Popup | window_flags

    id: pop_menu
    property int window_flags: 0
    property int element_height: 14
    property int context_count
    property QtObject pop_menu_model

    color: "white"

    signal menu_item_clicked(int index)

    Rectangle {
        id: rect
        border.color: "black"
        border.width: 1
        anchors.fill: parent

        Component {
            id: pop_delegte
            Item {
                id: wrapper
                width: pop_menu.width; height: pop_menu.element_height
                x: 5
                Text {
                    font.pixelSize: pop_menu.element_height - 4
                    text: name
                    anchors.verticalCenter: parent.verticalCenter
                }

                MouseArea {
                    anchors.fill: parent
                    onEntered: wrapper.ListView.view.currentIndex = index
                    hoverEnabled: true
                    onClicked: menu_item_clicked(index)
                }
            }
        }

        Component {
            id: highlight_bar
            Rectangle {
                width: rect.width; height: pop_menu.element_height + view.spacing
                color: "steelblue"
                y: view.currentItem.y - view.spacing / 2;
            }
        }

        ListView {
            id: view
//            width: pop_menu.width; height: pop_menu.height
            anchors.fill: parent
            focus: true

            model: pop_menu_model
            delegate: pop_delegte
            highlight: highlight_bar
            highlightFollowsCurrentItem: false

            spacing: 10
        }
    }
}
